{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Number Of Rectangles That Can Form The Largest Square"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: countGoodRectangles"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #可以形成最大正方形的矩形数目"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个数组 <code>rectangles</code> ，其中 <code>rectangles[i] = [l<sub>i</sub>, w<sub>i</sub>]</code> 表示第 <code>i</code> 个矩形的长度为 <code>l<sub>i</sub></code> 、宽度为 <code>w<sub>i</sub></code> 。</p>\n",
    "\n",
    "<p>如果存在 <code>k</code> 同时满足 <code>k <= l<sub>i</sub></code> 和 <code>k <= w<sub>i</sub></code> ，就可以将第 <code>i</code> 个矩形切成边长为 <code>k</code> 的正方形。例如，矩形 <code>[4,6]</code> 可以切成边长最大为 <code>4</code> 的正方形。</p>\n",
    "\n",
    "<p>设 <code>maxLen</code> 为可以从矩形数组 <code>rectangles</code> 切分得到的 <strong>最大正方形</strong> 的边长。</p>\n",
    "\n",
    "<p>请你统计有多少个矩形能够切出边长为<em> </em><code>maxLen</code> 的正方形，并返回矩形 <strong>数目</strong> 。</p>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>rectangles = [[5,8],[3,9],[5,12],[16,5]]\n",
    "<strong>输出：</strong>3\n",
    "<strong>解释：</strong>能从每个矩形中切出的最大正方形边长分别是 [5,3,5,5] 。\n",
    "最大正方形的边长为 5 ，可以由 3 个矩形切分得到。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>rectangles = [[2,3],[3,7],[4,3],[3,7]]\n",
    "<strong>输出：</strong>3\n",
    "</pre>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 <= rectangles.length <= 1000</code></li>\n",
    "\t<li><code>rectangles[i].length == 2</code></li>\n",
    "\t<li><code>1 <= l<sub>i</sub>, w<sub>i</sub> <= 10<sup>9</sup></code></li>\n",
    "\t<li><code>l<sub>i</sub> != w<sub>i</sub></code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [number-of-rectangles-that-can-form-the-largest-square](https://leetcode.cn/problems/number-of-rectangles-that-can-form-the-largest-square/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [number-of-rectangles-that-can-form-the-largest-square](https://leetcode.cn/problems/number-of-rectangles-that-can-form-the-largest-square/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[[5,8],[3,9],[5,12],[16,5]]', '[[2,3],[3,7],[4,3],[3,7]]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        res, maxLen = 0, 0\n",
    "        for l, w in rectangles:\n",
    "            k = min(l, w)\n",
    "            if k == maxLen:\n",
    "                res += 1\n",
    "            elif k > maxLen:\n",
    "                res = 1\n",
    "                maxLen = k\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        r = {}\n",
    "        ret = 0\n",
    "        p = 0\n",
    "        for l in rectangles:\n",
    "            s = min(l)\n",
    "            r[s] = r.get(s, 0) + 1\n",
    "            if s >= p:\n",
    "                ret = r[s]\n",
    "                p = s\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        maxLen = 0\n",
    "        for l,w in rectangles:\n",
    "            maxLen = max(maxLen,min(l,w))\n",
    "        #print(maxLen)\n",
    "        ans = 0\n",
    "        for l,w in rectangles:\n",
    "            if maxLen <= l and maxLen <= w:\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        maxN,ans = 0,0\n",
    "        for r in rectangles:\n",
    "            if min(r) > maxN:\n",
    "                maxN = min(r)\n",
    "                ans = 1\n",
    "            elif min(r) == maxN:\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        ns = [min(r) for r in rectangles]\n",
    "        return ns.count(max(ns))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        a=0\n",
    "        b=1\n",
    "        for r in rectangles:\n",
    "            if min(r)>a:\n",
    "                a=min(r)\n",
    "                b=1\n",
    "            elif min(r)==a:\n",
    "                b+=1\n",
    "        return b\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        n=len(rectangles)\n",
    "        for i in range(n):\n",
    "            rectangles[i]=min(rectangles[i][0],rectangles[i][1])\n",
    "        return rectangles.count(max(rectangles))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        k = max([min(i) for i in rectangles])  \n",
    "        ans = 0\n",
    "        for r in rectangles:\n",
    "            if k == min(r):\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        temp_dic=[0]\n",
    "        res_cnt=0\n",
    "        for r in rectangles:\n",
    "            temp_r=min(r)\n",
    "            if temp_r>temp_dic[-1]:\n",
    "                res_cnt=1\n",
    "                temp_dic[-1]=temp_r\n",
    "                continue\n",
    "            if temp_r==temp_dic[-1]:\n",
    "                res_cnt+=1\n",
    "            # print(res_cnt)\n",
    "        return res_cnt\n",
    "\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        m = map(min,rectangles)\n",
    "        d = Counter(m)\n",
    "        dic_max = max(d)\n",
    "        return d[dic_max]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        for i, x in enumerate(rectangles):\n",
    "            rectangles[i] = min(x)\n",
    "        return rectangles.count(max(rectangles))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        m = Counter(map(min, rectangles))\n",
    "        return m[max(m)]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        a = [min(x1,x2) for x1,x2 in rectangles]\n",
    "        return sum([1 for x in a if x==max(a)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        m = Counter(map(min, rectangles))\n",
    "        return m[max(m)]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        r=[min(i) for i in rectangles]\n",
    "        return r.count(max(r))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        ans, m = 1, 0\n",
    "        for a, b in rectangles:\n",
    "            n = min(a, b)\n",
    "            if n == m:\n",
    "                ans += 1\n",
    "            elif n > m:\n",
    "                ans = 1\n",
    "                m = n\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        counter = Counter(min(x) for x in rectangles)\n",
    "        return counter[max(counter)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        return sorted(collections.Counter(min(rec) for rec in rectangles).items(),key=lambda item: item[0],reverse = True)[0][1]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        maxLen = 0\n",
    "        cnt = 0\n",
    "        for rectangle in rectangles:\n",
    "            if min(rectangle[0], rectangle[1]) == maxLen:\n",
    "                cnt += 1\n",
    "            elif min(rectangle[0], rectangle[1]) > maxLen:\n",
    "                maxLen = min(rectangle[0], rectangle[1])\n",
    "                cnt = 1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        l=[]\n",
    "        for i in rectangles:\n",
    "            l.append(min(i))\n",
    "        cnt=Counter(l)\n",
    "        return cnt[max(l)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        maxLen = 0\n",
    "        cnt = 0\n",
    "        for w, l in rectangles:\n",
    "            k = min(w, l)\n",
    "            if k == maxLen: cnt += 1\n",
    "            elif k > maxLen:\n",
    "                maxLen = k\n",
    "                cnt = 1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        return (lambda lst: lst.count(max(lst)))([min(x,y) for x,y in rectangles])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        ma = max(min(l, w) for l, w in rectangles)\n",
    "\n",
    "        return sum(min(l, w) == ma for l, w in rectangles)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "      box_size = [min(rec) for rec in rectangles]\n",
    "      return box_size.count(max(box_size))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        cnt = Counter([min(rectangle) for rectangle in rectangles])\n",
    "\n",
    "        res = 0\n",
    "        maxLen = 0\n",
    "        for k, v in cnt.items():\n",
    "            if k > maxLen:\n",
    "                maxLen = k \n",
    "                res = v\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        max_len, ans = 0, 0\n",
    "        for x, y in rectangles:\n",
    "            l = min(x, y)\n",
    "            if l > max_len:\n",
    "                max_len = l\n",
    "                ans = 1\n",
    "            elif l == max_len:\n",
    "                ans += 1\n",
    "        \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        a = []\n",
    "        for i,j in rectangles:\n",
    "            a.append(min(i,j))\n",
    "        b = Counter(a)\n",
    "        return b[max(b)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        ans, max = 0, 0\n",
    "        for r in rectangles:\n",
    "            a = min(r[0], r[1])\n",
    "            if a == max:\n",
    "                ans += 1\n",
    "            elif a > max:\n",
    "                ans = 1\n",
    "                max = a\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        dic = {}\n",
    "        for item in rectangles:\n",
    "            m = min(item)\n",
    "            if m in dic:\n",
    "                dic[m] += 1\n",
    "            else:\n",
    "                dic[m] = 1\n",
    "        return dic[max(dic)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        count=0\n",
    "        maxmin=0\n",
    "        for i in rectangles:\n",
    "            if (m:=min(i))>maxmin:\n",
    "                maxmin=m\n",
    "                count=1\n",
    "            elif m==maxmin:\n",
    "                count+=1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        r=[min(i) for i in rectangles]\n",
    "        return r.count(max(r))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        r = [min(i) for i in rectangles]\n",
    "        return r.count(max(r))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        m = 0\n",
    "        lst = []\n",
    "        for a, b in rectangles:\n",
    "            n = min(a, b)\n",
    "            lst.append(n)\n",
    "            m = max(m, n)\n",
    "        return lst.count(m)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        l = []\n",
    "        for pair in rectangles:\n",
    "            l.append(min(pair))\n",
    "        l = sorted(l)\n",
    "        return Counter(l)[l[-1]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        res = []\n",
    "        n = len(rectangles)\n",
    "        for i in range(n):\n",
    "            t = min(rectangles[i])\n",
    "            res.append(t)\n",
    "        res.sort()\n",
    "        count = 0\n",
    "        for i in res:\n",
    "            if i == res[-1]:\n",
    "                count+= 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        res, tmp = 0, 0\n",
    "        for i,j in rectangles:\n",
    "            tmp1 = min(i,j)\n",
    "            if tmp1 > tmp:\n",
    "                res = 1\n",
    "                tmp = tmp1\n",
    "            elif tmp1 == tmp:\n",
    "                res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        x=[]\n",
    "        for each in rectangles:\n",
    "            x.append(min(each))\n",
    "        n=x.count(max(x))\n",
    "        return n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        l = [min(x[0], x[1]) for x in rectangles]\n",
    "        return l.count(max(l))\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        curr_max = 0\n",
    "        curr_cnt = 0\n",
    "        for r in rectangles:\n",
    "            m = min(r)\n",
    "            if m > curr_max:\n",
    "                curr_max = m\n",
    "                curr_cnt = 1\n",
    "            elif m == curr_max:\n",
    "                curr_cnt += 1\n",
    "        return curr_cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        m = {}\n",
    "        maximum = 0\n",
    "        for rect in rectangles:\n",
    "            w, h = rect[0], rect[1]\n",
    "            minimum = min(w, h)\n",
    "            if minimum not in m.keys():\n",
    "                m[minimum] = []\n",
    "            m[minimum].append(minimum)\n",
    "            maximum = max(maximum, minimum)\n",
    "        return len(m[maximum])\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        temp = []\n",
    "        for i in rectangles:\n",
    "            temp.append(min(i[0],i[1]))\n",
    "        temp.sort()\n",
    "        res = 0\n",
    "        for i in range(-1,-len(temp)-1,-1):\n",
    "            if temp[i] == temp[-1]:\n",
    "                res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        maxl = 0\n",
    "        c = 0\n",
    "        for i in rectangles:\n",
    "            cu = min(i)\n",
    "            if maxl < cu:\n",
    "                maxl = cu\n",
    "                c = 1\n",
    "            elif maxl == cu:\n",
    "                c += 1\n",
    "        return c\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        la=[min(i) for i in rectangles]\n",
    "        return la.count(max(la))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        ret = []\n",
    "        maxLen = 0\n",
    "        ans = 0\n",
    "        for r in rectangles:\n",
    "            curLen = min(r[0], r[1])\n",
    "            maxLen = max(maxLen, curLen)\n",
    "            ret.append(curLen)\n",
    "        return sum(1 for c in ret if c == maxLen)\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        l = [min(x[0], x[1]) for x in rectangles]\n",
    "        return l.count(max(l))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        c = Counter([min(item) for item in rectangles])\n",
    "        return c[max(c)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        m = dict()\n",
    "        max_len = 0\n",
    "        for  l, w in rectangles:\n",
    "            k = min(l, w)\n",
    "            m[k] = m.get(k, 0) + 1\n",
    "            max_len = max(max_len, k)\n",
    "        return m.get(max_len, -1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        r=[]\n",
    "        for i in rectangles:\n",
    "            r.append(min(i))\n",
    "        return r.count(max(r))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        maxLen = 0\n",
    "        cnt = 0\n",
    "        for rectangle in rectangles:\n",
    "            maxLen = max(maxLen, min(rectangle[0], rectangle[1]))\n",
    "        for rectangle in rectangles:\n",
    "            if maxLen <= min(rectangle[0], rectangle[1]):\n",
    "                cnt += 1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        maxlen=0\n",
    "        cnt=0\n",
    "        for i in rectangles:\n",
    "            if min(i)>maxlen:\n",
    "                maxlen=min(i)\n",
    "                cnt=0\n",
    "            elif min(i)==maxlen:\n",
    "                cnt+=1\n",
    "        return cnt+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        list1 = [min(i) for i in rectangles]\n",
    "        maxnum = max(list1)\n",
    "        return list1.count(maxnum)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        m = Counter(map(min, rectangles))\n",
    "        return m[max(m)]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        c = Counter([min(item) for item in rectangles])\n",
    "        return c[max(c)]\n",
    "        return c[c.most_common(1)[0][0]]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        cutted = [min(r) for r in rectangles]\n",
    "        max_val = max(cutted)\n",
    "        return cutted.count(max_val)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        rectangles = list(map(min, rectangles))\n",
    "        return rectangles.count(max(rectangles))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        val = cnt = 0\n",
    "        for i in range(len(rectangles)):\n",
    "            n = min(rectangles[i])\n",
    "            if n > val:\n",
    "                val = n\n",
    "                cnt =1\n",
    "            elif n == val:\n",
    "                cnt += 1\n",
    "\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        x=[min(i) for i in rectangles]\n",
    "        return x.count(max(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        maxSide = 0\n",
    "        maxSideCnt = 0\n",
    "\n",
    "        for x in rectangles:\n",
    "            side = min(x[0], x[1])\n",
    "            if side > maxSide: # we get a new maxSide\n",
    "                maxSide = side\n",
    "                maxSideCnt = 1\n",
    "            elif side == maxSide:\n",
    "                maxSideCnt += 1\n",
    "        \n",
    "        return maxSideCnt\n",
    "        \n",
    "        \n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        res,cnt=0,0\n",
    "        for x,y in rectangles:\n",
    "            cur=min(x,y)\n",
    "            if cur>res:\n",
    "                res=cur\n",
    "                cnt=1\n",
    "            elif cur==res:\n",
    "                cnt +=1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        maxLen = 0\n",
    "        cnt = 0\n",
    "        for rectangle in rectangles:\n",
    "            if min(rectangle[0], rectangle[1]) == maxLen:\n",
    "                cnt += 1\n",
    "            elif min(rectangle[0], rectangle[1]) > maxLen:\n",
    "                maxLen = min(rectangle[0], rectangle[1])\n",
    "                cnt = 1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        # cutted = [min(r) for r in rectangles]\n",
    "        # max_val = max(cutted)\n",
    "        # return cutted.count(max_val)\n",
    "\n",
    "        # ------------------------\n",
    "        return nums.count(max_val) if (nums := [min(r) for r in rectangles]) and (max_val := max(nums)) else 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "@staticmethod\n",
    "def min_edge(rec:List[int])-> int:\n",
    "    if rec[0]>rec[1]:\n",
    "        return rec[1]\n",
    "    else: \n",
    "        return rec[0]\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        max=0\n",
    "        count=0\n",
    "        for rec in rectangles:\n",
    "            if min_edge(rec)>=max:\n",
    "                max = min_edge(rec)\n",
    "        for rec in rectangles:\n",
    "            if rec[0]>=max and rec[1]>=max:\n",
    "                count+=1\n",
    "        return count\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        res=[]\n",
    "        for i in rectangles:\n",
    "            res.append(min(i))\n",
    "        c=Counter(res)\n",
    "        return c[max(res)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        r=[min(i) for i in rectangles]\n",
    "        return r.count(max(r))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        maxLen = 0\n",
    "        cnt = 0\n",
    "        for rectangle in rectangles:\n",
    "            minLen = min(rectangle)\n",
    "            if maxLen < minLen:\n",
    "                maxLen = minLen\n",
    "                cnt = 1\n",
    "            elif minLen == maxLen:\n",
    "                cnt += 1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        temp = []\n",
    "        for i in rectangles:\n",
    "            temp.append(min(i[0],i[1]))\n",
    "        temp.sort()\n",
    "        res = 0\n",
    "        for i in range(-1,-len(temp)-1,-1):\n",
    "            if temp[i] == temp[-1]:\n",
    "                res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        ans=[]\n",
    "        for list in rectangles:\n",
    "            ans.append(min(list))\n",
    "        return ans.count(max(ans))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        edge_lengths = {}\n",
    "        for rectangle in rectangles:\n",
    "            min_edge = min(rectangle)\n",
    "            edge_lengths[min_edge] = edge_lengths.get(min_edge, 0) + 1\n",
    "        max_key = 0 \n",
    "        for key in edge_lengths.keys():\n",
    "            if max_key < int(key):\n",
    "                max_key = int(key)\n",
    "        return edge_lengths[max_key]\n",
    "\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        x = 0\n",
    "        l = []\n",
    "        for pair in rectangles:\n",
    "            l.append(min(pair))\n",
    "        l = sorted(l)\n",
    "        return Counter(l)[l[-1]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        maxlen=0\n",
    "        count=0\n",
    "        for l,w in rectangles:\n",
    "            if min(l,w)>maxlen:\n",
    "                maxlen=min(l,w)\n",
    "        for l,w in rectangles:\n",
    "            if min(l,w)>= maxlen:\n",
    "                count+=1\n",
    "        return count\n",
    "\n",
    "\n",
    "\n",
    "            \n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        ans=[]\n",
    "        for rectangle in rectangles:\n",
    "            ans.append(min(rectangle))\n",
    "        return ans.count(max(ans))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n",
    "        res = []\n",
    "        n = len(rectangles)\n",
    "        for i in range(n):\n",
    "            t = min(rectangles[i])\n",
    "            res.append(t)\n",
    "        res.sort()\n",
    "        count = 0\n",
    "        for i in res:\n",
    "            if i == res[-1]:\n",
    "                count+= 1\n",
    "        return count"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
